package com.ttl.bc.ic.barcodemaster.action;


import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;

import com.ttl.bc.ic.stklocation.BCStkLocationBean;
import com.ttl.bc.ic.stklocation.BCStkLocationDAO;
import com.ttl.common.util.CommonUtils;
import com.ttl.common.util.Page;

public class BCBarcodeMasterAction extends DispatchAction {

	@SuppressWarnings({ "static-access", "unchecked" })
	public ActionForward barcode(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {

		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCBarcodeMasterAction #mode :barcode");		
				
		DynaActionForm sform = (DynaActionForm) form;		
		String itemFind = new String(sform.get("itemFind").toString().getBytes("ISO8859_1"),"UTF-8");
		String  shelfCode = sform.get("shelfCode").toString();

		if(shelfCode.equals("")) {
			shelfCode = "12";
		}
		String hql2 = "FROM BCStkLocationBean WHERE shelfCode='"+shelfCode+"'" +
		" and (itemCode like '2-%' or itemCode like '3-%' )";
		System.out.println(hql2);
		ArrayList al = (ArrayList)new BCStkLocationDAO().find(hql2);		
		System.out.println("number of items ="+al.size());		
		String hql = "FROM BCBarcodeMaster " +
						" WHERE (barcode like '%"+itemFind+"%' " +
								" or barcodeName like '%"+itemFind+"%' " +
								" or itemCode like '%"+itemFind+"%') ";// +
//						" and (itemCode like '2-%' or itemCode like '3-%' )" +
//						" ORDER BY itemCode";
		if(al.size()>0) {
			hql += " and itemCode in (";
			String it = "";
			int test = 0;
			for (int i = 0; i < al.size(); i++) {
				BCStkLocationBean itc = (BCStkLocationBean)al.get(i);
				it += "'"+itc.getItemCode()+"',";
				test++;
			}
			System.out.println("test = "+test);
			hql += it.substring(0, it.length()-1) +") ORDER BY itemCode";
		}
		System.out.println(hql);

		new Page().setPage(request, hql);
		
		request.setAttribute("itemFind", itemFind);		
		request.setAttribute("shelfCode", shelfCode);
		return mapping.findForward("blank_barcode");
	}	
	
	@SuppressWarnings("unchecked")
	public ActionForward generateBarcode(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
				
		String item = (String)request.getParameter("item");
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCBarcodeMasterAction #mode :generateBarcode ");		
		
		
//		DynaActionForm dynaForm = (DynaActionForm)form;
//		String barcode1 = dynaForm.getString("barcode1");
//		String barcode2 = dynaForm.getString("barcode2");
		
/*		ArrayList docNo1List = (ArrayList)new BCPurchaseOrderDAO().find("FROM BCPurchaseOrderQuery WHERE docNo = '"+docNo1+"'");
		int rowOrder1=0, rowOrder2=0;
		for (int i = 0; i < docNo1List.size(); i++) {
			BCPurchaseOrderQuery poQ = (BCPurchaseOrderQuery)docNo1List.get(i);
			rowOrder1 = poQ.getRowOrder().intValue();
		}
		ArrayList docNo2List = (ArrayList)new BCPurchaseOrderDAO().find("FROM BCPurchaseOrderQuery WHERE docNo = '"+docNo2+"'");
		for (int i = 0; i < docNo2List.size(); i++) {
			BCPurchaseOrderQuery poQ = (BCPurchaseOrderQuery)docNo2List.get(i);
			rowOrder2 = poQ.getRowOrder().intValue();			
		}		
		
		docNoParam = " po.rowOrder>= "+rowOrder1+" and po.rowOrder<="+rowOrder2;
		*/
		
		String itemParam = " itemCode in ("+item +") ";
		
//		
//		
//		String[] startDocNo = docNo1.split("-");
//		String[] endDocNo = docNo2.split("-");
//		String preDocNo = "";//startDocNo[0]+"-"+startDocNo[1]+"-"; 
//			
//		int countDocNo = 0;//Integer.valueOf(endDocNo[2])-Integer.valueOf(startDocNo[2])+1;
//		NumberFormat formatter = new DecimalFormat("0000");
//		int index1 = docNo1.indexOf("DC");
//		
//		if(index1!=-1) {// found "DC" in docNo1
//			preDocNo = startDocNo[0]+"-"+startDocNo[1]+"-"+startDocNo[2]+"-";
//			
//			formatter = new DecimalFormat("000");
//			countDocNo = Integer.valueOf(endDocNo[3])-Integer.valueOf(startDocNo[3])+1;
//			System.out.println("countDocNo = "+countDocNo);
//			for(int i=0; i<countDocNo; i++){
//				
//				int docs = (Integer.parseInt(startDocNo[3])+i);
//				System.out.println("docs= "+docs);
//				String docs2 = ""+formatter.format(docs);
//				docNoParam+="'"+preDocNo+docs2+"',";
//			}			
//			
//		}else {
//			preDocNo = startDocNo[0]+"-"+startDocNo[1]+"-";
//			
//			formatter = new DecimalFormat("0000");
//			countDocNo = Integer.valueOf(endDocNo[2])-Integer.valueOf(startDocNo[2])+1;
//			System.out.println("countDocNo = "+countDocNo);
//			for(int i=0; i<countDocNo; i++){	
//				int docs = (Integer.parseInt(startDocNo[2])+i);
//				System.out.println("docs= "+docs);
//				String docs2 = ""+formatter.format(docs);
//				docNoParam+="'"+preDocNo+docs2+"',";
//			}
//		}
//		
//
//		docNoParam = docNoParam.substring(0, docNoParam.length()-1  ) +")";
		
		System.out.println(itemParam);
//start จะทดสอบส่งค่า parameter แบบ list 		
//		String hql = "from BCPurchaseOrderBean where docNo = '"+docNoParam+"'";
//		BCPurchaseOrderBO bo = new BCPurchaseOrderBO();
//		ArrayList results = (ArrayList)bo.findPrint(hql);
//end จะทดสอบส่งค่า parameter แบบ list
		String nameOfReport="";
//		if(!stkDocNoParam.equalsIgnoreCase("null")) {
//			nameOfReport = "/ireport/poForm.jasper";
//			System.out.println("stkDocNo !=null");
//		}else {
			nameOfReport = "/ireport/barcode5.jasper";//last:	barcode2.jasper
//			System.out.println("stkDocNo ==null");
//		}
							
		HashMap hashMap = new HashMap();
		hashMap.put("itemParam", itemParam);		
		if(CommonUtils.runReportToPdf(response, getServlet(), hashMap, nameOfReport)==true) {
			//new BCPurchaseOrderDAO().addPrintCount(docNoParam);
			System.out.println("run report to pdf: complete");
		}else {
			System.out.println("run Report to pdf: can't run report");
		}
		
		
		return null;			
	}	
}
